
rm(list = ls())

library(tidyverse)
library(readxl)
library(writexl)
library(lubridate)

ukraine_raw <- read_excel("UkraineLawDataRaw.xlsx")

ukraine_temp <- ukraine_raw %>%
  select(start_time = V8, end_time = V9, randomid = RandomID,
         matches("S1|S2", ignore.case = FALSE), lab, session,
         matches("G1|G2|G3", ignore.case = FALSE),
         contains("lottery"), contains("PSM"), contains("big5"),
         matches("job_qualities|law_pref|law_likely"),
         matches("approval|law_|corruption|corrupt_"),
         age, male = gender, class_yr, fakultet,
         gpa = GPA, olympiad, contains("EGE"), region,
         city_size, contains("religion"), family_income,
         contains("relatives"), ethnic, lang,
         soc_science_knowledg, intro2, Intro
  )


#PRELIMINARY VARIABLE CONSTRUCTION FOR DICE TASK GAME

#changing variables names of individual virtual dice rolls
#g1.1...g1.30 are the first set of 20 dice rolls (each respondent sees only 20 of these 30 dice profiles)
#g2.1...g2.30 are the second set of 20 dice rolls (each respondent sees only 20 of these 30 dice profiles)

names(ukraine_temp) = gsub(pattern = "\\(", replacement = "", x = names(ukraine_temp)) 
names(ukraine_temp) = gsub(pattern = "\\)", replacement = "", x = names(ukraine_temp)) 
names(ukraine_temp) = gsub(pattern = "G2v1_T", replacement = "g1.", x = names(ukraine_temp)) 
names(ukraine_temp) = gsub(pattern = "G2v2_T", replacement = "g2.", x = names(ukraine_temp)) 
ukraine_temp <- ukraine_temp %>% dplyr::rename(g1.25 = G2v1F_T1, g1.26 = G2v1F_T2,  g1.27 = G2v1F_T3,  g1.28 = G2v1F_T4, g1.29 = G2v1F_T5 , g1.30 = G2v1F_T6)
ukraine_temp <- ukraine_temp %>% dplyr::rename(g2.25 = G2v2F_T1, g2.26 = G2v2F_T2,  g2.27 = G2v2F_T3,  g2.28 = G2v2F_T4, g2.29 = G2v2F_T5 , g2.30 = G2v2F_T6)

# correct guesses are already coded as 1, incorrect guesses as NA; changing here NA to 0
#ukraine_temp <- ukraine_temp %>% mutate_at(vars(starts_with('g', ignore.case = F), -contains("gpa")), funs(replace_na(., 0)))
#replaced the above line with the line below b/c "funs" deprecated
ukraine_temp <- ukraine_temp %>% mutate_at(vars(starts_with('g', ignore.case = F), -contains("gpa")), list(~replace_na(., 0)))

# correct_guesses1 is sum of correct guesses in first 20 dice rolls
dice_rolls1 <- ukraine_temp %>% select(starts_with('g1.', ignore.case = F))
ukraine_temp$correct_guesses1 <- rowSums(dice_rolls1)   

# correct_guesses2 is sum of correct guesses in second 20 dice rolls
dice_rolls2 <- ukraine_temp %>% select(starts_with('g2.', ignore.case = F))
ukraine_temp$correct_guesses2 <- rowSums(dice_rolls2)  


#PRELIMINARY VARIABLE CONSTRUCTION FOR BRIBERY GAME

ukraine_temp$bribe_accept_size1 = with(ukraine_temp,
                                          ifelse(G3_bur_br_350 > G3_bur_br_300, 7,
                                                 ifelse(G3_bur_br_300 > G3_bur_br_250, 6,
                                                        ifelse(G3_bur_br_250 > G3_bur_br_200, 5,
                                                               ifelse(G3_bur_br_200 > G3_bur_br_150, 4,
                                                                      ifelse(G3_bur_br_150 > G3_bur_br_100, 3,
                                                                             ifelse(G3_bur_br_100 > G3_bur_br_50, 2,
                                                                                    ifelse(G3_bur_br_50 > 0, 1, 0)))))))
)

#this version removes subjects with non-transitive preferences
ukraine_temp$bribe_accept_size2 = with(ukraine_temp,
                                          ifelse((G3_bur_br_50 <= G3_bur_br_100 & 
                                                    G3_bur_br_100 <= G3_bur_br_150 & 
                                                    G3_bur_br_150 <= G3_bur_br_200 & 
                                                    G3_bur_br_200 <= G3_bur_br_250 &
                                                    G3_bur_br_250 <= G3_bur_br_300 & 
                                                    G3_bur_br_300 <= G3_bur_br_350) == TRUE, bribe_accept_size1, NA)
)

#OTHER PRELIMINARY CLEANING

ukraine_temp <- ukraine_temp %>% mutate_at(vars(contains("_work_")), list(~replace_na(., 0)))



#CREATING DATA SET

ukraine_dta <- ukraine_temp %>% transmute(
  #DICTATOR GAME
  donate = G1_donate_1, 
  #converts to hryvnia amount
  donate5 = donate*5,
  donate_norm = (donate - min(donate))/(max(donate) - min(donate)),
  charity = recode(G1_charity, `1` = "Povernis zhivim", `2` = "Krab" ,`3` = "Dobrobyt gromad"),
  #DICE TASK GAME
  correct_guesses1, correct_guesses2,
  correct_guesses = correct_guesses1 + correct_guesses2,
  cheat_rate = (1/(1 - (1/6))) * 1/40 * correct_guesses - (1/6)/(1 - (1/6)),
  #BRIBERY GAME
  bribe_accept_size1,
  bribe_accept_size2,
  bribe_offer_size = G3_citizen_1,
  bribe_offer = ifelse(bribe_offer_size > 0, 1, 0),
  bribe_accept = ifelse(bribe_accept_size1 > 0, 1, 0),
  bribe = ifelse(is.na(bribe_offer) == FALSE, bribe_offer, bribe_accept),
  citizen_nobribe = G3_citizen_nobribe,
  bur_nobribe = G3_bur_nobribe,
  #RISK AVERSION MEASURE
  risk_averse1 = 8 - (lottery_1 + lottery_2 + lottery_3 + lottery_4 + lottery_5 + lottery_6 + lottery_7),
  #this version removes subjects with non-transitive preferences
  risk_averse2 = ifelse((lottery_1 <= lottery_2 & 
                           lottery_2 <= lottery_3 & 
                           lottery_3 <= lottery_4 &
                           lottery_4 <= lottery_5 &
                           lottery_5 <= lottery_6 & 
                           lottery_6 <= lottery_7) == TRUE, risk_averse1, NA),
  #CAREER PREFERENCE VARIABLES
    pros_pref = law_pref_1,
    inv_pref = law_pref_2,
    judge_pref = law_pref_3,
    govlaw_pref = law_pref_5,
    bailiff_pref = law_pref_7,
    deflaw_pref = law_pref_8,
    comlaw_pref = law_pref_9,
    notary_pref = law_pref_10,
    publaw_avg = (pros_pref + inv_pref + judge_pref + bailiff_pref + govlaw_pref)/5,
    publaw_avg2 = (pros_pref + inv_pref + judge_pref + bailiff_pref)/4,
    privlaw_avg = (deflaw_pref + comlaw_pref + notary_pref)/3,
    other_pref = ifelse(publaw_avg < mean(publaw_avg, na.rm = T) & privlaw_avg < mean(privlaw_avg, na.rm = T), 1, 0),
    pros_exp = law_likely_1,
    inv_exp = law_likely_2,
    judge_exp = law_likely_3,
    govlaw_exp = law_likely_5,
    bailiff_exp = law_likely_7,
    deflaw_exp = law_likely_8,
    comlaw_exp = law_likely_9,
    notary_exp = law_likely_10,
  publaw_exp_avg = (pros_exp + inv_exp + judge_exp + bailiff_exp + govlaw_exp)/5,
  publaw_exp_avg2 = (pros_exp + inv_exp + judge_exp + bailiff_exp)/4,
  privlaw_exp_avg = (deflaw_exp + comlaw_exp + notary_exp)/3,
  #JOB ATTRIBUTES
  job_stable = job_qualities_1,
  job_income = job_qualities_2,
  job_bens = job_qualities_3,
  job_promo = job_qualities_4,
  job_interest = job_qualities_5,
  job_altruism = job_qualities_6,
  job_social = job_qualities_7,
  job_schedule = job_qualities_8,
  job_connect = job_qualities_9,
  job_prestige = job_qualities_10,
  job_intrinsic = (job_social + job_altruism + job_interest)/3,
  job_extrinsic = (job_income + job_connect + job_prestige)/3,
  job_extrinsic2 = (job_income + job_connect + job_prestige + job_promo)/4,
  job_pragmatic = (job_stable + job_bens + job_schedule)/3,
  #JUSTIFYING BRIBERY
  corr_justify = corruption_3,
  corr_justify_dich = ifelse(corr_justify > 2, 1, 0), # median for corr_justify = 2
  #PSM
  aps1 = PSM_1, aps2 = PSM_2, aps3 = PSM_3, aps4 = PSM_4,
  cpv1 = PSM_5, cpv2 = PSM_6, cpv3 = PSM_7, cpv4 = PSM_8,
  com1 = PSM_9, com2 = PSM_10, com3 = PSM_11, com4 = PSM_12,
  ss1 = PSM_13, ss2 = PSM_14, ss3 = PSM_15, ss4 = PSM_16,
  aps = (aps1 + aps2 + aps3 + aps4)/4,
  cpv = (cpv1 + cpv2 + cpv3 + cpv4)/4,
  com = (com1 + com2 + com3 + com4)/4,
  ss = (ss1 + ss2 + ss3 + ss4)/4,
  psm = (aps + cpv + com + ss)/4,
  # DEMOGRAPHIC VARIABLES
  age, male, olympiad, region, 
  family_income = na_if(family_income, 99),
  city_size, religion, religion_services,
  religion_denom = recode(religion_denom, `1` = "Orthodox", `2` =  "Ukrainian Orthodox - Kyiv",
                          `3` = "Ukrainian Orthodox - Moscow", `4` = "Ukrainian Autocephalous Orthodo", `5` = "Uniate", 
                          `6` = "Islam", `7` = "Protestantism", `8` = "Catholicism", `9` =  "Judaism", `10` = "Other"),
  #correcting for error in Qualtrics variable coding
  gpa = dplyr::recode(gpa, `10` = 1, `1` = 2, `2` = 3, `3` = 4, `4` = 5, `5` = 6),
  zno_taken = EGE_taken,
  zno_math = na_if(EGE_3_TEXT, "-"),
  zno_math = na_if(zno_math, 0),
  zno_math = as.numeric(gsub(",", ".", zno_math)),
  zno_ukr = as.numeric(gsub(",", ".", EGE_10_TEXT)),
  zno_law = na_if(EGE_18_TEXT, "-"),
  zno_law = na_if(zno_law, 0),
  zno_law = as.numeric(gsub(",", ".", zno_law)),
  lang = recode(lang, `1` = "Russian", `2` = "Ukrainian", `3` = "Both", `4` = "Other"),
  ethnic = recode(ethnic, `1` = "Ukrainian", `2` = "Russian", `3` = "Tatar", `4` = "Bulgar",
                  `5` = "Polish", `6` = "Moldovan", `7` = "Belarussian", `8` = "Jewish", `9` = "Other"),
  f_work_pros = relatives_work_50_1, m_work_pros = relatives_work_50_2, r_work_pros = relatives_work_50_3,
  f_work_court = relatives_work_51_1, m_work_court = relatives_work_51_2, r_work_court = relatives_work_51_3,
  f_work_law = relatives_work_52_1, m_work_law = relatives_work_52_2, r_work_law = relatives_work_52_3,
  fam_law = ifelse(f_work_law == 1 | m_work_law == 1 | r_work_law == 1, 1, 0),
  fam_pros = ifelse(f_work_pros == 1 | m_work_pros == 1 | r_work_pros == 1, 1, 0),
  fam_court = ifelse(f_work_court == 1 | m_work_court == 1 | r_work_court == 1, 1, 0),
  fam_pros_court = ifelse(fam_pros == 1 | fam_court == 1, 1, 0),
  #collapsing all MA students together
  class_yr = recode(class_yr, `6` = 5),
  #dep
  dep_publaw = ifelse(fakultet == 3 | fakultet == 6, 0, 1),
  dep = fakultet,  
 #QUALITY CHECK VARIABLES
  randomid, lab, session,
  soc_sci_knowledge = soc_science_knowledg,
  #time to complete
  duration = round(difftime(end_time, start_time, units=c("auto","secs","mins","hours","days","weeks")), 0),
  #screeners
  screener1 = ifelse(is.na(S1_27) == F & is.na(S1_29) == F, 1, 0),
  screener2 = ifelse(is.na(S2_8) == F & is.na(S2_12) == F, 1, 0),
  screener = screener1 + screener2
)



### ADDING IN DICE ROLLS BY EACH SET OF 10 ROLLS RATHER THAN 20 ROLLS (at request of reviewer)
#Note: subjects engaged in 20 rolls at a time, so calculating correct guesses per first and second 20 rolls is more straightforward, per calculations above

#changing variables names of individual virtual dice rolls
#g1.1...g1.30 are the first set of 20 dice rolls (each respondent sees only 20 of these 30 dice profiles)
#g2.1...g2.30 are the second set of 20 dice rolls (each respondent sees only 20 of these 30 dice profiles)

names(ukraine_raw) = gsub(pattern = "\\(", replacement = "", x = names(ukraine_raw)) 
names(ukraine_raw) = gsub(pattern = "\\)", replacement = "", x = names(ukraine_raw)) 

names(ukraine_raw) = gsub(pattern = "G2v1_T", replacement = "g1.", x = names(ukraine_raw)) 
names(ukraine_raw) = gsub(pattern = "G2v2_T", replacement = "g2.", x = names(ukraine_raw)) 
ukraine_raw <- ukraine_raw %>% dplyr::rename(g1.25 = G2v1F_T1, g1.26 = G2v1F_T2,  g1.27 = G2v1F_T3,  g1.28 = G2v1F_T4, g1.29 = G2v1F_T5 , g1.30 = G2v1F_T6)
ukraine_raw <- ukraine_raw %>% dplyr::rename(g2.25 = G2v2F_T1, g2.26 = G2v2F_T2,  g2.27 = G2v2F_T3,  g2.28 = G2v2F_T4, g2.29 = G2v2F_T5 , g2.30 = G2v2F_T6)

## looking at incorrect reported guesses separately, b/c to break into first set of 10 rolls and second set of 10 rolls need to see the reported guesses in order to separate out dice profiles not seen from incorrect guesses (both are NA)
names(ukraine_raw) = gsub(pattern = "G2v1_F", replacement = "f1.", x = names(ukraine_raw)) 
names(ukraine_raw) = gsub(pattern = "G2v2_F", replacement = "f2.", x = names(ukraine_raw)) 

ukraine_raw <- ukraine_raw %>% dplyr::rename(f1.25 = G2v1F_F1, f1.26 = G2v1F_F2,  f1.27 = G2v1F_F3,  f1.28 = G2v1F_F4, f1.29 = G2v1F_F5 , f1.30 = G2v1F_F6)
ukraine_raw <- ukraine_raw %>% dplyr::rename(f2.25 = G2v2F_F1, f2.26 = G2v2F_F2,  f2.27 = G2v2F_F3,  f2.28 = G2v2F_F4, f2.29 = G2v2F_F5 , f2.30 = G2v2F_F6)

ukraine_raw <- ukraine_raw %>% mutate_at(vars(starts_with("f1.", ignore.case =F)), list(~ifelse(. == 1, 0, NA)))
ukraine_raw <- ukraine_raw %>% mutate_at(vars(starts_with("f2.", ignore.case =F)), list(~ifelse(. == 1, 0, NA)))


## FIRST 20 ROLLS

dice_rolls1_v1 <- ukraine_raw %>% select(starts_with('g1.', ignore.case = F))
dice_rolls1_v1 <- dice_rolls1_v1 %>% mutate_all(list(~as.numeric(.)))
ukraine_raw$correct_guesses1_v1 <- rowSums(dice_rolls1_v1, na.rm = T) 

dice_rolls1_v2 <- ukraine_raw %>% select(starts_with('g1.', ignore.case = F), starts_with('f1.', ignore.case = F))
dice_rolls1_v2 <- dice_rolls1_v2  %>% mutate_all(list(~as.numeric(.)))

dice_rolls1_v2 <- dice_rolls1_v2 %>% transmute(die1 = if_else(is.na(g1.1) == T, f1.1, g1.1),
                                               die2 = if_else(is.na(g1.2) == T, f1.2, g1.2),  
                                               die3 = if_else(is.na(g1.3) == T, f1.3, g1.3), 
                                               die4 = if_else(is.na(g1.4) == T, f1.4, g1.4),  
                                               die5 = if_else(is.na(g1.5) == T, f1.5, g1.5),  
                                               die6 = if_else(is.na(g1.6) == T, f1.6, g1.6),  
                                               die7 = if_else(is.na(g1.7) == T, f1.7, g1.7),  
                                               die8 = if_else(is.na(g1.8) == T, f1.8, g1.8),  
                                               die9 = if_else(is.na(g1.9) == T, f1.9, g1.9),  
                                               die10 = if_else(is.na(g1.10) == T, f1.10, g1.10),  
                                               die11 = if_else(is.na(g1.11) == T, f1.11, g1.11),  
                                               die12 = if_else(is.na(g1.12) == T, f1.12, g1.12),  
                                               die13 = if_else(is.na(g1.13) == T, f1.13, g1.13),  
                                               die14 = if_else(is.na(g1.14) == T, f1.14, g1.14),  
                                               die15 = if_else(is.na(g1.15) == T, f1.15, g1.15),  
                                               die16 = if_else(is.na(g1.16) == T, f1.16, g1.16),  
                                               die17 = if_else(is.na(g1.17) == T, f1.17, g1.17),  
                                               die18 = if_else(is.na(g1.18) == T, f1.18, g1.18),  
                                               die19 = if_else(is.na(g1.19) == T, f1.19, g1.19),  
                                               die20 = if_else(is.na(g1.20) == T, f1.20, g1.20),
                                               die21 = if_else(is.na(g1.21) == T, f1.21, g1.21),
                                               die22 = if_else(is.na(g1.22) == T, f1.22, g1.22),
                                               die23 = if_else(is.na(g1.23) == T, f1.23, g1.23),
                                               die24 = if_else(is.na(g1.24) == T, f1.24, g1.24),
                                               die25 = if_else(is.na(g1.25) == T, f1.25, g1.25),
                                               die26 = if_else(is.na(g1.26) == T, f1.26, g1.26),
                                               die27 = if_else(is.na(g1.27) == T, f1.27, g1.27),
                                               die28 = if_else(is.na(g1.28) == T, f1.28, g1.28),
                                               die29 = if_else(is.na(g1.29) == T, f1.29, g1.29),
                                               die30 = if_else(is.na(g1.30) == T, f1.30, g1.30)
)


dice_rolls1_v2$correct_guesses1_1st10 <- ifelse(rowSums(is.na(dice_rolls1_v2[,1:10]) == F) > 9, rowSums(dice_rolls1_v2[,1:10],na.rm = T),
                                                ifelse(rowSums(is.na(dice_rolls1_v2[,1:11]) == F) > 9, rowSums(dice_rolls1_v2[,1:11],na.rm = T),
                                                       ifelse(rowSums(is.na(dice_rolls1_v2[,1:12]) == F) > 9, rowSums(dice_rolls1_v2[,1:12],na.rm = T),
                                                              ifelse(rowSums(is.na(dice_rolls1_v2[,1:13]) == F) > 9, rowSums(dice_rolls1_v2[,1:13],na.rm = T),
                                                                     ifelse(rowSums(is.na(dice_rolls1_v2[,1:14]) == F) > 9, rowSums(dice_rolls1_v2[,1:14],na.rm = T),
                                                                            ifelse(rowSums(is.na(dice_rolls1_v2[,1:15]) == F) > 9, rowSums(dice_rolls1_v2[,1:15],na.rm = T)
                                                                            ))))))

dice_rolls1_v2$correct_guesses1_2nd10 <- ifelse(rowSums(is.na(dice_rolls1_v2[,1:10]) == F) > 9, rowSums(dice_rolls1_v2[,11:30],na.rm = T),
                                                ifelse(rowSums(is.na(dice_rolls1_v2[,1:11]) == F) > 9, rowSums(dice_rolls1_v2[,12:30],na.rm = T),
                                                       ifelse(rowSums(is.na(dice_rolls1_v2[,1:12]) == F) > 9, rowSums(dice_rolls1_v2[,13:30],na.rm = T),
                                                              ifelse(rowSums(is.na(dice_rolls1_v2[,1:13]) == F) > 9, rowSums(dice_rolls1_v2[,14:30],na.rm = T),
                                                                     ifelse(rowSums(is.na(dice_rolls1_v2[,1:14]) == F) > 9, rowSums(dice_rolls1_v2[,15:30],na.rm = T),
                                                                            ifelse(rowSums(is.na(dice_rolls1_v2[,1:15]) == F) > 9, rowSums(dice_rolls1_v2[,16:30],na.rm = T)
                                                                            ))))))

dice_rolls1_v2$correct_guesses1_v2 <- dice_rolls1_v2$correct_guesses1_1st10 + dice_rolls1_v2$correct_guesses1_2nd10

ukraine_dta$correct_guesses1_1st10   <- dice_rolls1_v2$correct_guesses1_1st10
ukraine_dta$correct_guesses1_2nd10   <- dice_rolls1_v2$correct_guesses1_2nd10


## SECOND 20 ROLLS

dice_rolls2_v1 <- ukraine_raw %>% select(starts_with('g2.', ignore.case = F))
dice_rolls2_v1 <- dice_rolls2_v1 %>% mutate_all(list(~as.numeric(.)))
ukraine_raw$correct_guesses2_v1 <- rowSums(dice_rolls2_v1, na.rm = T) 

dice_rolls2_v2 <- ukraine_raw %>% select(starts_with('g2.', ignore.case = F), starts_with('f2.', ignore.case = F))
dice_rolls2_v2 <- dice_rolls2_v2  %>% mutate_all(list(~as.numeric(.)))

dice_rolls2_v2 <- dice_rolls2_v2 %>% transmute(die1 = if_else(is.na(g2.1) == T, f2.1, g2.1),
                                               die2 = if_else(is.na(g2.2) == T, f2.2, g2.2),  
                                               die3 = if_else(is.na(g2.3) == T, f2.3, g2.3), 
                                               die4 = if_else(is.na(g2.4) == T, f2.4, g2.4),  
                                               die5 = if_else(is.na(g2.5) == T, f2.5, g2.5),  
                                               die6 = if_else(is.na(g2.6) == T, f2.6, g2.6),  
                                               die7 = if_else(is.na(g2.7) == T, f2.7, g2.7),  
                                               die8 = if_else(is.na(g2.8) == T, f2.8, g2.8),  
                                               die9 = if_else(is.na(g2.9) == T, f2.9, g2.9),  
                                               die10 = if_else(is.na(g2.10) == T, f2.10, g2.10),  
                                               die11 = if_else(is.na(g2.11) == T, f2.11, g2.11),  
                                               die12 = if_else(is.na(g2.12) == T, f2.12, g2.12),  
                                               die13 = if_else(is.na(g2.13) == T, f2.13, g2.13),  
                                               die14 = if_else(is.na(g2.14) == T, f2.14, g2.14),  
                                               die15 = if_else(is.na(g2.15) == T, f2.15, g2.15),  
                                               die16 = if_else(is.na(g2.16) == T, f2.16, g2.16),  
                                               die17 = if_else(is.na(g2.17) == T, f2.17, g2.17),  
                                               die18 = if_else(is.na(g2.18) == T, f2.18, g2.18),  
                                               die19 = if_else(is.na(g2.19) == T, f2.19, g2.19),  
                                               die20 = if_else(is.na(g2.20) == T, f2.20, g2.20),
                                               die21 = if_else(is.na(g2.21) == T, f2.21, g2.21),
                                               die22 = if_else(is.na(g2.22) == T, f2.22, g2.22),
                                               die23 = if_else(is.na(g2.23) == T, f2.23, g2.23),
                                               die24 = if_else(is.na(g2.24) == T, f2.24, g2.24),
                                               die25 = if_else(is.na(g2.25) == T, f2.25, g2.25),
                                               die26 = if_else(is.na(g2.26) == T, f2.26, g2.26),
                                               die27 = if_else(is.na(g2.27) == T, f2.27, g2.27),
                                               die28 = if_else(is.na(g2.28) == T, f2.28, g2.28),
                                               die29 = if_else(is.na(g2.29) == T, f2.29, g2.29),
                                               die30 = if_else(is.na(g2.30) == T, f2.30, g2.30)
)


dice_rolls2_v2$correct_guesses2_3rd10 <- ifelse(rowSums(is.na(dice_rolls2_v2[,1:10]) == F) > 9, rowSums(dice_rolls2_v2[,1:10],na.rm = T),
                                                ifelse(rowSums(is.na(dice_rolls2_v2[,1:11]) == F) > 9, rowSums(dice_rolls2_v2[,1:11],na.rm = T),
                                                       ifelse(rowSums(is.na(dice_rolls2_v2[,1:12]) == F) > 9, rowSums(dice_rolls2_v2[,1:12],na.rm = T),
                                                              ifelse(rowSums(is.na(dice_rolls2_v2[,1:13]) == F) > 9, rowSums(dice_rolls2_v2[,1:13],na.rm = T),
                                                                     ifelse(rowSums(is.na(dice_rolls2_v2[,1:14]) == F) > 9, rowSums(dice_rolls2_v2[,1:14],na.rm = T),
                                                                            ifelse(rowSums(is.na(dice_rolls2_v2[,1:15]) == F) > 9, rowSums(dice_rolls2_v2[,1:15],na.rm = T)
                                                                            ))))))

dice_rolls2_v2$correct_guesses2_4th10 <- ifelse(rowSums(is.na(dice_rolls2_v2[,1:10]) == F) > 9, rowSums(dice_rolls2_v2[,11:30],na.rm = T),
                                                ifelse(rowSums(is.na(dice_rolls2_v2[,1:11]) == F) > 9, rowSums(dice_rolls2_v2[,12:30],na.rm = T),
                                                       ifelse(rowSums(is.na(dice_rolls2_v2[,1:12]) == F) > 9, rowSums(dice_rolls2_v2[,13:30],na.rm = T),
                                                              ifelse(rowSums(is.na(dice_rolls2_v2[,1:13]) == F) > 9, rowSums(dice_rolls2_v2[,14:30],na.rm = T),
                                                                     ifelse(rowSums(is.na(dice_rolls2_v2[,1:14]) == F) > 9, rowSums(dice_rolls2_v2[,15:30],na.rm = T),
                                                                            ifelse(rowSums(is.na(dice_rolls2_v2[,1:15]) == F) > 9, rowSums(dice_rolls2_v2[,16:30],na.rm = T)
                                                                            ))))))

dice_rolls2_v2$correct_guesses2_v2 <- dice_rolls2_v2$correct_guesses2_3rd10 + dice_rolls2_v2$correct_guesses2_4th10


ukraine_dta$correct_guesses2_3rd10   <- dice_rolls2_v2$correct_guesses2_3rd10
ukraine_dta$correct_guesses2_4th10   <- dice_rolls2_v2$correct_guesses2_4th10

ukraine_dta = ukraine_dta %>% mutate(
      cheat_rate1st10 = (1/(1 - (1/6))) * 1/10 * correct_guesses1_1st10 - (1/6)/(1 - (1/6)),
      cheat_rate2nd10 = (1/(1 - (1/6))) * 1/10 * correct_guesses1_2nd10 - (1/6)/(1 - (1/6)),
      cheat_rate3rd10 = (1/(1 - (1/6))) * 1/10 * correct_guesses2_3rd10 - (1/6)/(1 - (1/6)),
      cheat_rate4th10 = (1/(1 - (1/6))) * 1/10 * correct_guesses2_4th10 - (1/6)/(1 - (1/6)),
                      )


#converting tibble to dataframe
UkraineClean_DF <- as.data.frame(ukraine_dta)

#write_csv(UkraineClean_DF, "UkraineClean.csv", na = "")

